﻿@model string
<script type="text/javascript">
    (function ($) {
        $.fn.moveContent = function () {
            return this.each(function () {
                $($(this)[0]).droppable({
                    accept: "tr",
                    drop: function (e, ui) {
                        var uuid = typeof ($($(ui.draggable)[0])).attr('id') != "undefined" ? $($(ui.draggable)[0]).attr('id') : "";
                        var parentUUID = typeof ($(this)).attr('id') != "undefined" ? $(this).attr('id') : "";
                        var parentChain = typeof ($(this)).attr('parentChain') != "undefined" ? $(this).attr('parentChain') : "";
                        if (parentChain.indexOf(uuid) == -1) {
                            $.ajax({
                                url: '@Html.Raw(Server.HtmlDecode(this.Url.Action("MoveContent", ViewContext.RequestContext.AllRouteValues().Merge("UUID", "").Merge("ParentUUID", ""))))',
                                data: { uuid: uuid, parentUUID: parentUUID },
                                type: 'post',
                                beforeSend: function () {
                                    kooboo.cms.ui.loading().show();
                                },
                                success: function (response) {
                                    if (response.Success) {
                                        setTimeout(function () {
                                            document.location.reload(true);
                                            kooboo.cms.ui.messageBox().show('Succeed in moving the item', 'info');
                                        }, 400);
                                    } else {
                                        kooboo.cms.ui.messageBox().showResponse(response);
                                    }
                                    kooboo.cms.ui.loading().hide();
                                },
                                error: function () {
                                    kooboo.cms.ui.messageBox().show('There is an error occurs', 'error');
                                }
                            });
                        }
                        else {
                            //kooboo.cms.ui.messageBox().show('Move failed!', 'error');
                        }
                    },
                    hoverClass: "accept"
                });
            });
        }
    })(jQuery);

    (function ($) {
        $.fn.lazyTreeNode = function () {
            this.find('.expander').click(function () {
                var handler = $(this);
                var td = handler.parent();
                var tr = handler.parent().parent();
                var collapsed = tr.hasClass('collapsed');
                var childNode = tr.data('childNode');
                if (collapsed) {

                    if (childNode != null) {
                        childNode.show();
                    }
                    else {
                        var id = tr.attr('id');
                        var parentChain = tr.attr('parentChain');
                        var url = '@Html.Raw(Url.Action("QueryByParentUUID", ViewContext.RequestContext.AllRouteValues()))';
                        kooboo.cms.ui.loading().show();
                        $.ajax(url,
                        {
                            type: 'POST',
                            data: 'parentUUID=' + id + "&parentChain=" + (parentChain == undefined ? "" : parentChain),
                            success: function (data, textStatus, jqXHR) {
                                if (data.Model.length == 0) {
                                    tr.removeClass('parent');
                                }
                                else {
                                    var template = $('#treeNode-template').clone();
                                    ko.applyBindings(data, template[0]);
                                    var tbody = template.children('tbody');

                                    var parentPadding = parseInt(td.css("padding-left").replace("px", ""));
                                    tbody.find('.treeStyle').css('padding-left', parentPadding + 18);
                                    var html = $(tbody.html());
                                    tr.after(html);
                                    html.lazyTreeNode();
                                    html.dialogLink();
                                    html.moveContent();

                                    //used in the Categories.ascx
                                    parent.jQuery(tr.parent().parent()[0]).triggerHandler('treeNodeLoaded', [html]);

                                    tr.data('childNode', html);
                                }
                                kooboo.cms.ui.loading().hide();
                            }
                        });
                    }

                    tr.removeClass('collapsed').addClass('expanded');
                }
                else {
                    while (childNode != null) {
                        childNode.hide();
                        childNode.removeClass('expanded').addClass('collapsed');
                        childNode = childNode.data('childNode');
                    }
                    tr.removeClass('expanded').addClass('collapsed');
                }
            });
        };
    })(jQuery);

</script>

<script type="text/javascript">
    $(function () {
        $('.table-container').lazyTreeNode();
        @if (string.IsNullOrEmpty(Model) || Model.ToLower() != "readonly")
        {
            <text>
        var table = $("div.table-container table");
        var tbody = table.find('tbody:eq(1)').sortable({
            revert: true,
            handle: 'td:eq(0)',
            cancel: 'tr.folderTr',
            start: function (event, ui) {
                ui.placeholder.html('<td colspan="100"></td>');
            },
            placeholder: "ui-state-highlight holder",
            cursor: 'move'
        });
        $(".table-container tr:not(.folderTr)").moveContent();
        </text>
        }
    });
</script>
